package it.enea.xlab.validation;



import java.io.File;

import it.enea.xlab.validation.common.PropertiesUtil;
import it.enea.xlab.validation.controllers.common.WebControllersUtilities;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.xlab.file.XLabFileManager;

/**
 * @author Cristiano Novelli
 */
public class ValidationManagerImplITCase {

	// Interfaces Declarations
	static ValidationController validationManagerController;

	// Logger
	private static Logger logger = Logger.getLogger(ValidationManagerImplITCase.class);


	@Test
	public void test() {

		logger.info("***********************************");
		logger.info("*** ValidationManagerImplITCase ***");
		logger.info("***********************************");
		logger.info("");
		
		
		logger.info("1) ValidationController Retrieving...");
		validationManagerController = (ValidationController) WebControllersUtilities.getManager(ValidationController.CONTROLLER_NAME);
		Assert.assertNotNull(validationManagerController);	
		logger.info("OK ValidationController!");
		logger.info("");
		
		
		
		logger.info("2) File System Info Retrieving...");
		// i.e. "Validation_Artifacts"
		String validationArtifactsDir = PropertiesUtil.getArtifactsDirProperty();
		logger.info("OK Validation Directory Name: " + validationArtifactsDir);

		// Absolute Path
		String validationArtifactsAbsDirPath = PropertiesUtil.getArtifactsDirPath();
		boolean fileExists = XLabFileManager.isFileOrDirectoryPresent(validationArtifactsAbsDirPath);
		Assert.assertTrue(fileExists);
		logger.info("OK Validation Directory Path: " + validationArtifactsAbsDirPath);

		// i.e. "Validation_Artifacts/ubl-invoice.xml"
		String xml_valid = validationArtifactsDir.concat("/docs/ubl-invoice.xml");
		String xml_invalid = validationArtifactsDir.concat("/docs/ubl-invoice_withError.xml");
		
		
		
		String schema = "http://winter.bologna.enea.it/peppol_schema_rep/xsd/maindoc/UBL-Invoice-2.0.xsd";
		logger.info("XML VALID: " + xml_valid);
		logger.info("XML INVALID: " + xml_invalid);
		
		if (this.isFileOrDirectoryPresent(xml_valid) && this.isFileOrDirectoryPresent(xml_invalid)) {
		
			logger.info("FILES FOUND!");
			logger.info("SCHEMA: " + schema);
			logger.info("OK File System Info Retrieving");
			logger.info("");
			
			XErrorMessage[] emList = null;
			
			
			logger.info("3) Set Temporary Directory");
			
			logger.info("Temporary Directory: " + validationArtifactsDir.concat("/"));
			logger.info("");
			
			
			
			try {		
			
				logger.info("4) nothing() service call...");
				logger.info("(The service nothing is a service that returns a Long -1 to check the service framework)");
				Long result = validationManagerController.nothing();
				Assert.assertTrue(result.intValue() == -1);
				
				logger.info("OK nothing() service call");			
				logger.info("");	
				
				
				logger.info("5) Validation INVALID file...");
				
				
				//// VALIDATION INVALID ////
				emList = validationManagerController.validation(xml_invalid, schema);
				
				
				//emList = this.validation(xml, schema);
				logger.info("OK validation service call");
				//Assert.assertTrue(emList.length > 0);
				
				// Print Result
				this.printResult(emList, false);
				
			} catch (Exception e) {
				emList = null;
				logger.error("EXCEPTION on reportValidation CALL!");
				logger.error(e.getMessage());
				e.printStackTrace();
			}
	

		
			try {		
				logger.info("");
				logger.info("6) Validation VALID file...");
				
				//// VALIDATION VALID ////
				emList = validationManagerController.validation(xml_valid, schema);
				//emList = this.validation(xml, schema);
				logger.info("OK validation service call");

				// Print Result
				this.printResult(emList, true);
				
			} catch (Exception e) {
				emList = null;
				logger.error("EXCEPTION on reportValidation CALL!");
				logger.error(e.getMessage());
				e.printStackTrace();
			}
		
		} // END IF FILE ARE FOUND
		else
			logger.info("ERROR: FILE NOT FOUND!");
		
		logger.info("");
		logger.info("***********************************");
		logger.info("");
	
	} // END Test

	/*public ErrorMessage[] validation(String xmlRelPathFileName, String xsdURL) {

		ErrorMessage[] emList = null;
		
		String validationArtifactsDir = PropertiesUtil.getArtifactsDirProperty();
		
		SchemaMainValidator smv = new SchemaMainValidatorImpl();
		smv.setRootDir("");

		try {

			emList = smv.reportValidation(xmlRelPathFileName, xsdURL);
			
		} catch (LocalFileNotFoundException e) {
			emList = null;
		} catch (SchemaParserException e) {
			emList = null;
		} catch (NullPointerException e) {
			emList = null;
		}
		
		return emList;
	}*/
	
	private boolean isFileOrDirectoryPresent(String srcStr) {
		
		boolean result = false;

		File src = new File(srcStr);
		
		if (src.isFile() || src.isDirectory()) 
			result = true;		
		
		return result;		
	}	
	
	
	private void printResult(XErrorMessage[] emList, boolean isValid) {
		
		if (emList != null) {
			
			if (emList.length >0) {
				
				if (isValid)
					logger.info("FAILURE: a NOT EMPTY result for a VALID file implies a WRONG RESULT OF TEST!");
				else
					logger.info("SUCCESS: a NOT EMPTY result for an INVALID file implies a CORRECT RESULT OF TEST!");
				
				int i=0;
				while (i<emList.length){
					
					logger.info("TYPE " + i + ": " + emList[i].getErrorType());
					logger.info("RIGA " + i + ": " + emList[i].getLineNumber());
					logger.info("COL " + i + ": " + emList[i].getColNumber());
					logger.info("DESC " + i + ": " + emList[i].getDescription());
					
					i++;
				}
				
			} else {
				
				logger.info("emList.length == 0");
				if (isValid)
					logger.info("SUCCESS: an EMPTY result for a VALID file implies a CORRECT RESULT OF TEST!");
				else
					logger.info("FAILURE: an EMPTY result for an INVALID file implies a WRONG RESULT OF TEST!");
			}
		}
		else
			logger.warn("emList == null");	
	
	}
	
}




